
package managedbean;

import java.io.Serializable;
import java.util.*;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.*;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;

import jpa.CustomerJPA;
import jpa.ShippingAddressJPA;
import ejb.CustomerFacadeRemote;
import ejb.CustomerFacade;

/**
 * Managed Bean SetShippingAddressMBean
 */
@ManagedBean(name = "modifyShipping")
@SessionScoped
public class SetShippingAddress implements Serializable{
	private static final long serialVersionUID = 1L;

	@EJB
	private CustomerFacadeRemote customerRemote;
	
	protected CustomerJPA dataCustomer = new CustomerJPA();
	protected ShippingAddressJPA shipping = new ShippingAddressJPA();
	
	public SetShippingAddress(){
		dataCustomer = (CustomerJPA) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("loginController");
	}
	public CustomerJPA getDataCustomer()
	{
		return dataCustomer;
	}	
	public void setDataCustomer(CustomerJPA customer)
	{
		this.dataCustomer=customer;
	}
	public ShippingAddressJPA getShipping()
	{
		return shipping;
	}	
	public void setShipping(ShippingAddressJPA shipping)
	{
		this.shipping=shipping;
	}
	public String ListShipping(){
		dataCustomer = (CustomerJPA) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("loginController");
		if(dataCustomer.getShippingAddress()==null)
			shipping = new ShippingAddressJPA();
		else
			shipping=dataCustomer.getShippingAddress();
		return "shippingAddressView";
	}
	public String back(){
		shipping = null;
		dataCustomer = null;
		return "clientDetailView";
	}
	
	/**
	 * Method to get the parameters
	 */
	public String getShippingAddress(FacesContext fc){
 		Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
 		return params.get("addressSA");
 	}
	public String getShippingCity(FacesContext fc){
 		Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
 		return params.get("citySA");
 	}
	public String getShippingPostalCode(FacesContext fc){
 		Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
 		return params.get("postalCodeSA");
 	}
	
	
	public void setShippingAddressMBean() throws Exception{
		FacesContext fc = FacesContext.getCurrentInstance();
		shipping.setAddress(getShippingAddress(fc));
		shipping.setCity(getShippingCity(fc));
		shipping.setPostalCode(getShippingPostalCode(fc));

		if(shipping.getAddress().isEmpty()||shipping.getCity().isEmpty()||shipping.getPostalCode().isEmpty()){
			FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "You have to fill all the gaps.", null);
			FacesContext.getCurrentInstance().addMessage(null, msg);
			return;
		}
		
		setShippingAddress();
		
		dataCustomer.setShippingAddress(shipping);
		customerRemote.modifyData(dataCustomer);
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("loginController", dataCustomer);

	}
	
	public void setShippingAddress() throws Exception{
		
		Properties props = System.getProperties();
		Context ctx = new InitialContext(props);
		customerRemote = (CustomerFacadeRemote) ctx.lookup("java:app/PracticalCaseStudyJEE.jar/CustomerFacadeBean!ejb.CustomerFacadeRemote");
		if(dataCustomer.getShippingAddress()==null){
			shipping.setid(customerRemote.counter());
			customerRemote.addShippingAddress(shipping);
		}
		else{
			shipping.setid(dataCustomer.getShippingAddress().getid());
			customerRemote.updateShippingAddress(shipping);
		}

	}
}
